package com.tripupdater.view
{
	import com.tripupdater.event.TUEvent;
	import com.tripupdater.event.TUEventDispatcher;
	import com.tripupdater.type.CommandType;
	import com.tripupdater.type.SkypeStatusType;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;

	[Event(name="skypeStatusLoaded", type="com.tripupdater.event.TUEvent")]
	public class SkypeView extends MovieClip
	{
		private var skypeOffline:MovieClip;
		private var skypeOnline:MovieClip;
		private var skypeLoading:MovieClip;
		
		public function SkypeView()
		{
			super();
			
			createChildren();
		}
		
		private function createChildren():void
		{
			skypeOffline= 	getChildByName("skypeOfflineInstance") as MovieClip;
			skypeOnline= 	getChildByName("skypeOnlineInstance") as MovieClip;
			skypeLoading= 	getChildByName("skypeSigningInstance") as MovieClip;

			skypeOffline.visible= false;
			skypeOnline.visible= false;
			skypeLoading.visible= false;
		}
		
		public function getSkypeStatus():void
		{
			skypeLoading.visible= true;
			skypeLoading.addEventListener(Event.ENTER_FRAME, loopLoading);
			TUEventDispatcher.getInstance().dispatchEvent(new TUEvent(CommandType.GET_SKYPE_STATUS, null, this));
		}
		
		public function setSkypeStatus(status:String):void
		{
			skypeLoading.visible= false;
			skypeOffline.visible= false;
			skypeOnline.visible= false;
			
			switch(status)
			{
				case SkypeStatusType.ONLINE:
				case SkypeStatusType.AWAY:
				case SkypeStatusType.BUSY:
					skypeOnline.visible= true;
					skypeOnline.useHandCursor= true;
					skypeOnline.buttonMode= true;
					skypeOnline.addEventListener(MouseEvent.CLICK, onClick);
					break;
				case SkypeStatusType.OFFLINE:
				default:
					skypeOffline.visible= true;
					break;
			}
			
			dispatchEvent(new TUEvent(TUEvent.SKYPE_STATUS_LOADED));
		}
		
		private function onClick(e:MouseEvent):void
		{
			ExternalInterface.call("window.open", "skype:Lexico?call");
		}
		
		private function loopLoading(e:Event):void
		{
			skypeLoading.getChildByName("skypeArrowInstance").rotation+= 10;
		}
	}
}